package com.kxmall.admin.controller.vehicle.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxmall.capacity.domain.vo.KxCapacityBoxVo;
import com.kxmall.carousel.domain.vo.KxCarouselVo;
import com.kxmall.common.core.domain.PageQuery;
import com.kxmall.common.core.page.TableDataInfo;
import com.kxmall.common.utils.BeanCopyUtils;
import com.kxmall.product.domain.KxStoreCategory;
import com.kxmall.product.mapper.KxStoreCategoryMapper;
import com.kxmall.vehicle.domain.KxVehicleBoxGoods;
import com.kxmall.vehicle.domain.bo.KxVehicleBoxGoodsBo;
import com.kxmall.vehicle.domain.bo.KxVehicleInGoodsBo;
import com.kxmall.vehicle.domain.bo.WarningVehicleBo;
import com.kxmall.vehicle.domain.vo.KxVehicleBoxGoodsVo;
import com.kxmall.vehicle.domain.vo.KxVehicleBoxUpGoodsVo;
import com.kxmall.vehicle.domain.vo.KxVehicleInGoodsVo;
import com.kxmall.vehicle.mapper.KxVehicleBoxGoodsMapper;
import com.kxmall.admin.controller.vehicle.service.IKxVehicleGoodsService;
import com.kxmall.vehicle.mapper.KxVehicleBoxUpGoodsMapper;
import com.kxmall.vehicle.mapper.KxVehicleInGoodsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * 餐车商品Service业务层处理
 */
@RequiredArgsConstructor
@Service
public class KxVehicleGoodsServiceImpl implements IKxVehicleGoodsService {

    @Autowired
    private KxVehicleBoxUpGoodsMapper vehicleBoxUpGoodsMapper;

    @Autowired
    private KxVehicleBoxGoodsMapper vehicleBoxGoodsMapper;

    @Autowired
    private KxVehicleInGoodsMapper vehicleInGoodsMapper;

    @Autowired
    private KxStoreCategoryMapper storeCategoryMapper;

    /**
     * 查询餐车商品
     */
    @Override
    public KxVehicleBoxGoodsVo queryById(Long id) {
        return vehicleBoxGoodsMapper.selectVoById(id);
    }

    /**
     * 查询餐车商品列表
     */
    @Override
    public TableDataInfo<KxVehicleBoxGoodsVo> queryPageList(KxVehicleBoxGoodsBo bo, PageQuery pageQuery) {
        List<KxVehicleBoxGoodsVo> vehicleBoxGoodsVos = new ArrayList<>();
        LambdaQueryWrapper<KxVehicleBoxGoods> lqw = buildQueryWrapper(bo);
        Page<KxVehicleBoxGoods> vehicleBoxGoodsList = vehicleBoxGoodsMapper.selectPage(pageQuery.build(), lqw);
        if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(vehicleBoxGoodsList.getRecords())){
            for(KxVehicleBoxGoods vehicleBoxGoods : vehicleBoxGoodsList.getRecords()){
                KxVehicleBoxGoodsVo vehicleBoxGoodsVo = new KxVehicleBoxGoodsVo();
                BeanCopyUtils.copy(vehicleBoxGoods, vehicleBoxGoodsVo);
                if((vehicleBoxGoodsVo.getBoxStatus() == 0)
                        &&(vehicleBoxGoodsVo.getBoxStates() == 1)
                        &&(vehicleBoxGoodsVo.getUpGoodsId() != null)
                        &&(vehicleBoxGoodsVo.getUpGoodsId() != 0)) {
                    KxVehicleBoxUpGoodsVo vehicleBoxInGoodsVo = vehicleBoxUpGoodsMapper.selectVoById(vehicleBoxGoodsVo.getUpGoodsId());
                    if(vehicleBoxGoodsVo != null) {
                        vehicleBoxGoodsVo.setGoodsNumbers(vehicleBoxInGoodsVo.getGoodsNumbers());
                        vehicleBoxGoodsVo.setSalesStatus(vehicleBoxInGoodsVo.getSalesStatus());
                        vehicleBoxGoodsVo.setUpPerson(vehicleBoxInGoodsVo.getUpPerson());
                        vehicleBoxGoodsVo.setUpTime(vehicleBoxInGoodsVo.getUpTime());
                        vehicleBoxGoodsVo.setUpDay(vehicleBoxInGoodsVo.getUpDay());
                        vehicleBoxGoodsVo.setRemarks(vehicleBoxInGoodsVo.getRemarks());
                        vehicleBoxGoodsVo.setInGoodsId(vehicleBoxInGoodsVo.getInGoodsId());
                        vehicleBoxGoodsVo.setCount(vehicleBoxInGoodsVo.getCount());
                        vehicleBoxGoodsVo.setPrice(vehicleBoxInGoodsVo.getPrice());
                        vehicleBoxGoodsVo.setAmount(vehicleBoxInGoodsVo.getAmount());
                        KxVehicleInGoodsVo vehicleInGoodsVo = vehicleInGoodsMapper.selectVoById(vehicleBoxInGoodsVo.getInGoodsId());
                        if(vehicleInGoodsVo != null) {
                            vehicleBoxGoodsVo.setInVehicleOrder(vehicleInGoodsVo.getInVehicleOrder());
                            vehicleBoxGoodsVo.setInVehicleCount(vehicleInGoodsVo.getInVehicleCount());
                            vehicleBoxGoodsVo.setProductId(vehicleInGoodsVo.getProductId());
                            vehicleBoxGoodsVo.setProductName(vehicleInGoodsVo.getProductName());
                            vehicleBoxGoodsVo.setProductAttrId(vehicleInGoodsVo.getProductAttrId());
                            vehicleBoxGoodsVo.setProductAttrName(vehicleInGoodsVo.getProductAttrName());
                            vehicleBoxGoodsVo.setCategoryId(vehicleInGoodsVo.getCategoryId());
                            vehicleBoxGoodsVo.setCategoryName(vehicleInGoodsVo.getCategoryName());
                            vehicleBoxGoodsVo.setOrPrice(vehicleInGoodsVo.getOrPrice());
                            //vehicleBoxGoodsVo.setBarCode(vehicleInGoodsVo.getBarCode());
                            vehicleBoxGoodsVo.setStock(vehicleInGoodsVo.getStock());
                            vehicleInGoodsVo.setSalesStatus(vehicleInGoodsVo.getSalesStatus());
                        }
                    }
                }
                vehicleBoxGoodsVos.add(vehicleBoxGoodsVo);
            }
        }
        return TableDataInfo.build(vehicleBoxGoodsVos);
    }

    /**
     * 查询餐车商品列表
     */
    @Override
    public List<KxVehicleBoxGoodsVo> queryList(KxVehicleBoxGoodsBo bo) {
        LambdaQueryWrapper<KxVehicleBoxGoods> lqw = buildQueryWrapper(bo);
        List<KxVehicleBoxGoods> vehicleBoxGoodsList = vehicleBoxGoodsMapper.selectList(lqw);
        List<KxVehicleBoxGoodsVo> vehicleBoxGoodsVos = new ArrayList<>();
        if(org.apache.commons.collections4.CollectionUtils.isNotEmpty(vehicleBoxGoodsList)){
            for(KxVehicleBoxGoods vehicleBoxGoods : vehicleBoxGoodsList){
                KxVehicleBoxGoodsVo vehicleBoxGoodsVo = new KxVehicleBoxGoodsVo();
                BeanCopyUtils.copy(vehicleBoxGoods, vehicleBoxGoodsVo);
                if((vehicleBoxGoodsVo.getBoxStatus() == 0)
                        &&(vehicleBoxGoodsVo.getBoxStates() == 1)
                        &&(vehicleBoxGoodsVo.getUpGoodsId() != null)
                        &&(vehicleBoxGoodsVo.getUpGoodsId() != 0)) {
                    KxVehicleBoxUpGoodsVo vehicleBoxInGoodsVo = vehicleBoxUpGoodsMapper.selectVoById(vehicleBoxGoodsVo.getUpGoodsId());
                    if(vehicleBoxGoodsVo != null) {
                        vehicleBoxGoodsVo.setGoodsNumbers(vehicleBoxInGoodsVo.getGoodsNumbers());
                        vehicleBoxGoodsVo.setSalesStatus(vehicleBoxInGoodsVo.getSalesStatus());
                        vehicleBoxGoodsVo.setUpPerson(vehicleBoxInGoodsVo.getUpPerson());
                        vehicleBoxGoodsVo.setUpTime(vehicleBoxInGoodsVo.getUpTime());
                        vehicleBoxGoodsVo.setUpDay(vehicleBoxInGoodsVo.getUpDay());
                        vehicleBoxGoodsVo.setRemarks(vehicleBoxInGoodsVo.getRemarks());
                        vehicleBoxGoodsVo.setInGoodsId(vehicleBoxInGoodsVo.getInGoodsId());
                        vehicleBoxGoodsVo.setCount(vehicleBoxInGoodsVo.getCount());
                        vehicleBoxGoodsVo.setPrice(vehicleBoxInGoodsVo.getPrice());
                        vehicleBoxGoodsVo.setAmount(vehicleBoxInGoodsVo.getAmount());
                        KxVehicleInGoodsVo vehicleInGoodsVo = vehicleInGoodsMapper.selectVoById(vehicleBoxInGoodsVo.getInGoodsId());
                        if(vehicleInGoodsVo != null) {
                            vehicleBoxGoodsVo.setInVehicleOrder(vehicleInGoodsVo.getInVehicleOrder());
                            vehicleBoxGoodsVo.setInVehicleCount(vehicleInGoodsVo.getInVehicleCount());
                            vehicleBoxGoodsVo.setProductId(vehicleInGoodsVo.getProductId());
                            vehicleBoxGoodsVo.setProductName(vehicleInGoodsVo.getProductName());
                            vehicleBoxGoodsVo.setProductAttrId(vehicleInGoodsVo.getProductAttrId());
                            vehicleBoxGoodsVo.setProductAttrName(vehicleInGoodsVo.getProductAttrName());
                            vehicleBoxGoodsVo.setCategoryId(vehicleInGoodsVo.getCategoryId());
                            vehicleBoxGoodsVo.setCategoryName(vehicleInGoodsVo.getCategoryName());
                            vehicleBoxGoodsVo.setOrPrice(vehicleInGoodsVo.getOrPrice());
                            //vehicleBoxGoodsVo.setBarCode(vehicleInGoodsVo.getBarCode());
                            vehicleBoxGoodsVo.setStock(vehicleInGoodsVo.getStock());
                            vehicleInGoodsVo.setSalesStatus(vehicleInGoodsVo.getSalesStatus());
                        }
                    }
                }
                vehicleBoxGoodsVos.add(vehicleBoxGoodsVo);
            }
        }
        return vehicleBoxGoodsVos;
    }

    private LambdaQueryWrapper<KxVehicleBoxGoods> buildQueryWrapper(KxVehicleBoxGoodsBo bo) {
        LambdaQueryWrapper<KxVehicleBoxGoods> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, KxVehicleBoxGoods::getId, bo.getId());
        lqw.eq(bo.getVehicleId() != null, KxVehicleBoxGoods::getVehicleId, bo.getVehicleId());
        lqw.eq(bo.getVehicleNo() != null, KxVehicleBoxGoods::getVehicleNo, bo.getVehicleNo());
        lqw.eq(bo.getVehicleName() != null, KxVehicleBoxGoods::getVehicleName, bo.getVehicleName());
        lqw.eq(bo.getBoxIndex() != null, KxVehicleBoxGoods::getBoxIndex, bo.getBoxIndex());
        lqw.eq(bo.getBoxStatus() != null, KxVehicleBoxGoods::getBoxStatus, bo.getBoxStatus());
        lqw.eq(bo.getBoxStates() != null, KxVehicleBoxGoods::getBoxStates, bo.getBoxStates());
        return lqw;
    }

    /**
     * 新增餐车商品
     */
    @Override
    public Boolean insertByBo(KxVehicleInGoodsBo bo) {
        KxVehicleBoxGoods add = BeanUtil.toBean(bo, KxVehicleBoxGoods.class);
        add.setId(0L);
        validEntityBeforeSave(add);
        boolean flag = vehicleBoxGoodsMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改餐车商品
     */
    @Override
    public Boolean updateByBo(KxVehicleInGoodsBo bo) {
        KxVehicleBoxGoods update = BeanUtil.toBean(bo, KxVehicleBoxGoods.class);
        validEntityBeforeSave(update);
        return vehicleBoxGoodsMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KxVehicleBoxGoods entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除餐车商品
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return vehicleBoxGoodsMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean freezeOrActivation(KxVehicleInGoodsBo bo) {
        KxVehicleBoxGoods vehicleBoxGoods = new KxVehicleBoxGoods();

        vehicleBoxGoods.setUpdateTime(new Date());
        vehicleBoxGoods.setId(bo.getId());
        return vehicleBoxGoodsMapper.update(vehicleBoxGoods, new LambdaQueryWrapper<KxVehicleBoxGoods>().eq(KxVehicleBoxGoods::getId, bo.getId())) > 0;
    }

    @Override
    public Boolean updateByVehicle(KxVehicleInGoodsBo bo) {
        return null;
    }

    @Override
    public Boolean updatePrice(KxVehicleInGoodsBo bo) {
        KxVehicleBoxGoods update = new KxVehicleBoxGoods();
        update.setUpdateTime(new Date());
        //update.setPrice(bo.getPrice());
        update.setId(bo.getId());
        return vehicleBoxGoodsMapper.update(update, new LambdaQueryWrapper<KxVehicleBoxGoods>().eq(KxVehicleBoxGoods::getId, bo.getId())) > 0;
    }

    @Override
    public TableDataInfo<KxVehicleBoxGoodsVo> queryPageWarningList(WarningVehicleBo bo, PageQuery pageQuery) {
        //类目查询
        LinkedList<Long> childrenIds = new LinkedList<>();
        if (bo.getCategoryId() != null && bo.getCategoryId() != 0L) {
            List<KxStoreCategory> childrenList = storeCategoryMapper.selectList(new LambdaQueryWrapper<KxStoreCategory>().eq(KxStoreCategory::getPid, bo.getCategoryId()));
            if (!CollectionUtils.isEmpty(childrenList)) {
                //一级分类
                childrenList.forEach(item -> childrenIds.add(item.getId()));
                //使用in查询，就不需要等于查询
                bo.setCategoryId(null);
            }
        }
        Integer offset = (pageQuery.getPageNum() - 1) * pageQuery.getPageSize();
        Integer size = pageQuery.getPageSize();
        //查询制定页数的库存预警
        //List<KxVehicleBoxGoodsVo> result = vehicleMapper.warningListByVehiclePage(offset,size, bo);
        //Long count = vehicleMapper.warningListByVehiclePageCount(bo);
        //return new TableDataInfo<KxVehicleBoxGoodsVo>(result,count);
        return null;
    }

    @Override
    public Boolean warningUpdate(WarningVehicleBo bo) {
        KxVehicleBoxGoods update = new KxVehicleBoxGoods();
        update.setUpdateTime(new Date());
        //update.setWarningNum(bo.getNum());
//        return vehicleMapper.update(update, new LambdaQueryWrapper<KxVehicleBoxGoods>()
//                                        .eq(KxVehicleBoxGoods::getVehicleId, bo.getVehicleId())
//                                        .eq(KxVehicleBoxGoods::getProductId,bo.getProductId())
//                                        .eq(KxVehicleBoxGoods::getProductAttrId,bo.getProductAttrId())) > 0;
        return false;
    }


}
